Skip to content

Mark static_empty_bucket_ptr storage as const(expr) - #95

Open
jwnimmer-tri wants to merge 1 commit into
Tessil:masterfrom
jwnimmer-tri:constexpr-static_empty_bucket_ptr
Open

Mark static_empty_bucket_ptr storage as const(expr)#95
jwnimmer-tri wants to merge 1 commit into
Tessil:masterfrom
jwnimmer-tri:constexpr-static_empty_bucket_ptr

Conversation

@jwnimmer-tri

Copy link
Copy Markdown

Even though the returned pointer must be non-const, it would be a mistake if any code in this library ever wrote into it. Marking the storage const (and casting away the const-ness on return) is a clear way to indicate that. (It may even show the fault during unit testing in case such a bug was ever introduced.)

Furthermore, when C++20 is enabled we can mark it constexpr, which means that the storage is defined as part of the compiled library's readonly data and doesn't need the atomic-initialize-upon-first-use guard variable. This also helps to avoid ODR hazards when linking this header-only library into multiple shared libraries (as can happen with nanobind when in NB_STATIC mode).

While we're here, we might as well mark all static constants constexpr as a cleanup -- even those not needed by the bucket_entry code.

Even though the returned pointer must be non-const, it would be a
mistake if any code in this library ever wrote into it. Marking the
storage const (and casting away the const-ness on return) is a clear
way to indicate that. (It may even show the fault during unit testing
in case such a bug was ever introduced.)

Furthermore, when C++20 is enabled we can mark it constexpr, which
means that the storage is defined as part of the compiled library's
readonly data and doesn't need the atomic-initialize-upon-first-use
guard variable. This also helps to avoid ODR hazards when linking this
header-only library into multiple shared libraries (as can happen with
nanobind when in NB_STATIC mode).

While we're here, we might as well mark all static constants constexpr
as a cleanup -- even those not needed by the bucket_entry code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant